#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QTcpSocket>
#include <QJsonParseError>
#include <QJsonObject>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("SamrtHouse");
    this->resize(320, 480);
    m_socket = NULL;
    connected = false;
    fanOpened = false;
    ui->ledButton->setEnabled(false);
    ui->tmpHumButton->setEnabled(false);
    ui->pushButton_5->setIcon(QIcon(":/icons/connected"));
    ui->ledButton->setIcon(QIcon(":/icons/update"));
    ui->tmpHumButton->setIcon(QIcon(":/icons/update"));
    ui->fanButton->setIcon(QIcon(":/icons/fan_open"));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_5_clicked()
{
    if (!connected){
        m_socket = new QTcpSocket(this);
        QString ipStr = ui->lineEdit->text();
        if (ipStr.isEmpty()){
            QMessageBox::warning(this, "IP error", "please input correct ip address.",
                                 QMessageBox::Cancel, QMessageBox::Cancel);
        }
        if (m_socket != NULL){
            m_socket->abort();
            m_socket->connectToHost(ipStr, 8888);
        }
        connect(m_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
        ui->lineEdit->setEnabled(false);
        ui->pushButton_5->setIcon(QIcon(":/icons/disconnected"));
        ui->ledButton->setEnabled(true);
        ui->tmpHumButton->setEnabled(true);
        ui->fanButton->setEnabled(true);
        connected = true;
    }else if (connected){
        m_socket->disconnectFromHost();
        delete m_socket;
        m_socket = NULL;
        ui->lineEdit->setEnabled(true);
        ui->pushButton_5->setIcon(QIcon(":/icons/connected"));
        connected = false;
        ui->ledButton->setEnabled(false);
        ui->tmpHumButton->setEnabled(false);
        ui->fanButton->setEnabled(false);
    }
}

void Widget::readyRead()
{
    QByteArray msg = m_socket->readAll();

    QJsonDocument document;
    QJsonParseError error;

    document = QJsonDocument::fromJson(msg, &error);
    if (error.error == QJsonParseError::NoError){
        QJsonObject root = document.object();
        QString target = root.take("target").toString();
        if (target == "humTemp"){
            QJsonObject result = root.take("result").toObject();
            int temp = result.take("tempture").toInt();
            int hum = result.take("humidity").toInt();
            QString time = result.take("time").toString();
            ui->tempLabel->setText(QString::number(temp));
            ui->humLabel->setText(QString::number(hum));
            ui->timeLabel->setText(time);
        }

    }else{
        QMessageBox::warning(this, "error", "read data from server error",
                             QMessageBox::Cancel, QMessageBox::Cancel);
    }
}

void Widget::on_ledButton_clicked()
{
    bool led[4];

    led[0] = ui->led1Check->isChecked();
    led[1] = ui->led2Check->isChecked();
    led[2] = ui->led3Check->isChecked();
    led[3] = ui->led4Check->isChecked();


    m_socket->write(QString("{ \"cmd\": \"setLeds\", \"leds\": [\"%1\", \"%2\", \"%3\", \"%4\"]}")
                    .arg(led[0]?"On" : "Off").arg(led[1]?"On":"Off")
                    .arg(led[2]?"On":"Off").arg(led[3]?"On":"Off").toUtf8());

}

void Widget::on_tmpHumButton_clicked()
{
    m_socket->write("{\"cmd\" : \"getTempHum\"}");
}

void Widget::on_fanButton_clicked()
{
    if (fanOpened){
        m_socket->write("{\"cmd\" : \"setFan\", \"fan\" : \"off\"}");
        fanOpened = false;
        ui->fanButton->setIcon(QIcon(":/icons/fan_open"));
    }else{
        m_socket->write("{\"cmd\" : \"setFan\", \"fan\" : \"on\"}");
        fanOpened = true;
        ui->fanButton->setIcon(QIcon(":/icons/fan_close"));
    }
}
